﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using KerriganDataContract;
using System.Diagnostics;
using KerriganDataContract.Code;

namespace KerriganService.DAC
{
    public class CodeDAC
    {

        public static Boolean AddCode(CodeDC bo)
        {

            TCode entity = new TCode();
            Boolean result = false;
            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    entity = CodeDAC.CopyBoToEntity(bo);
                    db.TCodes.AddObject(entity);
                    db.SaveChanges();
                    result = true;
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }
            return result;
        }

        public static int DeletCode(int id)
        {
            TCode entity = new TCode();
            int result = 0;
            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    entity = db.TCodes.FirstOrDefault(f => f.IdCode == id);
                    db.TCodes.DeleteObject(entity);
                    db.SaveChanges();
                    result = entity.IdCode;

                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }
            return result;
        }

        public static List<CodeDC> GetCodeByUser(int id)
        {
            List<CodeDC> list = new List<CodeDC>();
            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    list = CopyListEntityToListBo((from code in db.TCodes
                                                   where code.IdCustomerFK == id
                                                   select code).ToList());
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }
            return list;
        }










        public static CodeDC CopyEntityToBo(TCode entity)
        {
            CodeDC bo = new CodeDC();

            try
            {
                bo.IdCode = entity.IdCode;
                bo.Date = entity.Date;
                bo.Score = entity.Score;
                bo.IdCustomer = entity.IdCustomerFK;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return bo;
        }

        public static List<CodeDC> CopyListEntityToListBo(List<TCode> entities)
        {
            List<CodeDC> bo = new List<CodeDC>();
            try
            {
                foreach (TCode entity in entities)
                {
                    bo.Add(CopyEntityToBo(entity));
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return bo;
        }


        public static TCode CopyBoToEntity(CodeDC bo)
        {
            TCode entity = new TCode();

            try
            {
                entity.IdCode = bo.IdCode;
                entity.Date = bo.Date;
                entity.Score = bo.Score;
                entity.IdCustomerFK = bo.IdCustomer;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return entity;
        }
    }
}